import org.cyberneko.html.parsers.SAXParser

class BabyCenterController {

  def index = { }
  static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']
  /**
   * 各个阶段通过热门栏目抓取的内容
   */
  
  def hotcatalog() {
    def hostNum = new String[5]
    /*
    准备怀孕  itemData.size() = 3  parserPath =
     */
    hostNum[0] = "/preconception"
    /*
    孕期、0-1、1-3、3-6 itemData.size() = 2 parserPath =
     */
    hostNum[1] = "/pregnancy"
    hostNum[2] = "/baby"
    hostNum[3] = "/toddler"
    hostNum[4] = "/preschooler"
    def urlPrefix = "http://cn.babycenter.com"
    def entryList = new ArrayList()    
//    hostNum.each {
//      def itemHost = urlPrefix + it
      def itemHost = urlPrefix + hostNum[0] //range the index yourself to run
      println itemHost
      def parser = new SAXParser()
      parser.setFeature('http://xml.org/sax/features/namespaces', false)
      // itemPage: declared for parsered item page
      def itemPage = new XmlParser(parser).parse(itemHost)
      //itemData: ArrayList of item collections
      def itemData = itemPage.depthFirst().DIV.grep {it.'@class' == 'twoColBox'}
      println itemData.size()
      //i: declared to memory the size of div fullfill the condition div.'@class' == 'twoColBox'
      int i = itemData.size()
      def itemList = new ArrayList()
      if( i==2 &&itemHost.indexOf("preschooler")== -1){
        itemData[i-1].DIV.UL.A.each {
          def urlMap = [:]
          println it
          urlMap.url = urlPrefix + it.'@href'
          urlMap.title = it.text()
          itemList.add(urlMap)
        }
      } else {
        println "in else"
        itemData[i-1].DIV.A.each {
          def urlMap = [:]
          println it
          urlMap.url = urlPrefix + it.'@href'
          urlMap.title = it.text()
          itemList.add(urlMap)
        }
      }
      itemList.each {
        println "in each"
        def entryHost = it.url
        println entryHost
        def entryPage = new XmlParser(parser).parse(entryHost)
        def entryData = entryPage.depthFirst().DIV.grep {it.'@class' == 'twoColBox'}
        //println entryData
        entryData.each {
          //println it.UL.LI.A.size()
          it.UL.LI.A.each {link ->
            def entryUrlMap = [:]
            def href = urlPrefix + link.'@href'
            def content = getcontent(href)
            if(content != null){
              entryUrlMap.sourceUrl = href
              entryUrlMap.title = link.text()
              entryUrlMap.source = "babycenter"
              entryUrlMap.content = smoothcontent(content)
              if(link.'@href'.indexOf("illness") != -1){
                entryUrlMap['catalog.id'] = '12'
              } else {
                entryUrlMap['catalog.id'] = '3'
              }
              entryList.add(entryUrlMap)
            }
          }
        }
      }
//  } // end hostNum.each
    //render 'OK'
    return entryList
  }
  
  /**
   * 通过stage的option抓取的内容
   */
  def stage() {
    def stageHost = "http://cn.babycenter.com/stages"
    def parser = new SAXParser()
    parser.setFeature('http://xml.org/sax/features/namespaces', false)
    def stagePage = new XmlParser(parser).parse(stageHost)
    def data = stagePage.depthFirst().DIV.grep {it.'@class' == 'stageDropContainer'}
    println data.size()
    println data.FORM.SELECT
    println data[0]
    println data[0].FORM.SELECT.OPTION
    println data[0].FORM.SELECT.OPTION.size()
    def entryList = new ArrayList()
    data.each {div ->
      div.FORM.SELECT.OPTION.each {select ->
        def entryUrlMap = [:]
        def href = "http://cn.babycenter.com" + select.'@value'
        def content = getcontent(href)
        println "content: "+content
        if(content != null){
          entryUrlMap.sourceUrl = href
          entryUrlMap.title = select.text()
          entryUrlMap.source = "babycenter"
          entryUrlMap['catalog.id'] = '11'
          entryUrlMap.content = smoothcontent(content)
          entryList.add(entryUrlMap)
        }
      }
    }
    return entryList
  }

  def expert() {
   def expertHost = "http://cn.babycenter.com/expert"
    def parser = new SAXParser()
    parser.setFeature('http://xml.org/sax/features/namespaces', false)
    def expertPage = new XmlParser(parser).parse(expertHost)
    def data = expertPage.depthFirst().UL.grep {it.'@class' == 'articleLists'}
    println data.size()
    def entryList = new ArrayList()
    data.LI.A.each{
        def linkArray = it
        for(int k=0;k<linkArray.size();k++){
          def link = linkArray[k]
          def entryUrlMap = [:]
          def href = "http://cn.babycenter.com" + link.'@href'
          def content = getcontent(href)
          println "content: "+content
          if(content != null){
            entryUrlMap.sourceUrl = href
            entryUrlMap.title = link.text()
            entryUrlMap.source = "babycenter"
            entryUrlMap['catalog.id'] = '2'
            entryUrlMap.content = smoothcontent(content)
            entryList.add(entryUrlMap)
          }
        }
    }
    return entryList 
  }

  def dosave = {
    //def entryList = stage()
    //def entryList = hotcatlog()
    def entryList = hotcatalog()
    entryList.each {
      println it
      def entryInstance = Entry.findBySourceUrl(it.sourceUrl)
      if(entryInstance){
        //判断catalog.id是否等于'2'只有在是expert()的时候才判断
//        if(entryInstance.catalog.id != '2' ){
//          entryInstance.catalog = Catalog.get(2)
//          entryInstance.depth = entryInstance.catalog.depth
//          entryInstance.save(flush:true)
//          render ' updated '
//        } else {
//          render ' already exists '
//        }
         entryInstance.content = it.content
         entryInstance.save(flush:true)
         render 'already exists'
      } else {
        def entry = new Entry(it)
        if (entry.catalog) {
          entry.depth = entry.catalog.depth
        }
        entry.save(flush:true)
        if (!entry.hasErrors() && entry.save()) {
          render "save Ok"
        } else {
          render entry.errors
        }
      }
    }
  }

  def getcontent(String url) {
    println url
    def buffer = new StringBuffer()
    def parser = new SAXParser()
    def chapter = new XmlParser(parser).parse(url)
    def chapterDate = chapter.depthFirst().DIV.grep {it.'@class' == 'mainContentPad artExpContent' || it.'@class' == 'mainContentPad'}
    println "chapterDate.size() : " + chapterDate.size()
    def size = chapterDate.size()
    if(size == 0) println "size= 0 :" + url
    if (!chapterDate) {
      return null
    } else {
      chapterDate[0].children().each {
        if (it instanceof String) {
          buffer << it
        } else if (it.name().toString().indexOf("BR") != -1) {
          buffer << "<br/>"
        } else if (it.name().toString().indexOf("DIV") != -1) {
          // nothing to do
        } else if (it.name().toString().indexOf("H1") != -1) {
          // nothing to do
        } else if(it.name().toString().indexOf("FORM") != -1){
          //nothing to do
        } else if (it.name().toString().indexOf("H2") != -1) {
          buffer << "<H2>" << it.text() << "</H2>"
        } else if (it.name().toString().indexOf("P") != -1) {
          if (it.id != "writtenForCredit") {
            it.children().each {child ->
              if (child instanceof String) {
                buffer << child
              } else if (child.name().toString().indexOf("BR") != -1) {
                buffer << "<br/>"
              } else if (child.name().toString().indexOf("A") != -1) {
                println child.'@href'
                println ("about/mab/#huangxinghua/".indexOf("about/mab")!=-1)
                if (child.'@href') {
                  if ((child.'@href'.indexOf("about/mab") != -1)|| child.'@href'.indexOf('member') != -1) {
                    def href = child.'@href'
                    println href.toString()+"*****"
                    //ingnore the endnotes
                  } else {
                    buffer << child.text()
                  }
                }
              } else {
                buffer << child.text()
              }
            }
          }
        } else if (it.name().toString().indexOf("A") != -1) {
          if (it.'@href') {
            def href = it.'@href'
            println href
            if ((href.indexOf("about/mab")!= -1) || href.indexOf("member") != -1) {
              // ingnore the endnotes
              println (href+":: wx")
            } else {
              buffer << it.text()
            }
          } else {
            if (it.'@class' == null && it.'@id' == null)
              buffer << it.text()
          }
        }
      }
    }
    return buffer.toString()
  }

  def smoothcontent(String content) {
      content = content.replace("上一周", "")
      content = content.replace("下一周", "")
      content = content.replace("上一月", "")
      content = content.replace("下一月", "")
      content = content.replace("审阅：", "")
      return content.trim()
  }

  def test = {
//    def url = "http://cn.babycenter.com/pregnancy/antenataltests/overweight_scan/"
//    def content = getcontent(url)
//    println content
//    render 'test OK'
    def entryList = Entry.findAllByContentLike("%%")
    entryList.each {
       println it
    }
    println entryList.size()
    render entryList.size()
  }

  //parser babytree

}
